from flask import jsonify

def log_joystick_action(joystick_type, x, y):
    """
    记录摇杆动作
    """
    print(f"{joystick_type}摇杆坐标: X={x:.2f}, Y={y:.2f}", flush=True)

def handle_joystick_data(data):
    """
    处理摇杆数据
    :param data: 包含摇杆类型和坐标的字典
    :return: 处理结果
    """
    try:
        joystick_type = data.get('type')  # 'left' 或 'right'
        x = data.get('x')
        y = data.get('y')

        if joystick_type not in ['left', 'right']:
            return jsonify({
                'status': 'error',
                'message': '无效的摇杆类型'
            }), 400

        if x is None or y is None:
            return jsonify({
                'status': 'error',
                'message': '缺少坐标数据'
            }), 400

        # 记录摇杆动作
        log_joystick_action(joystick_type, x, y)

        # 简化响应，减少数据传输
        return jsonify({
            'status': 'success'
        })

    except Exception as e:
        return jsonify({
            'status': 'error',
            'message': str(e)
        }), 500 